package com.biz.primus.management.depot.controller;

import com.biz.primus.common.vo.SpringPageVO;
import com.biz.primus.management.base.controller.AbstractBaseController;
import com.biz.primus.management.depot.feign.DepotFeignClient;
import com.biz.primus.management.depot.feign.GeoFeignClient;
import com.biz.primus.model.depot.enums.BusinessStatus;
import com.biz.primus.model.depot.vo.DepotDetailRespVo;
import com.biz.primus.model.depot.vo.DepotListRespVo;
import com.biz.primus.model.depot.vo.DepotQueryReqVo;
import com.biz.primus.model.geo.vo.AreaResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * 门店管理---控制器
 *
 * @Auther JKLiues
 * @Date 2017/9/21
 */
@Controller
@RequestMapping("depots")
@Secured("ROLE_DEPOT")
public class DepotController extends AbstractBaseController {

    private final DepotFeignClient depotFeignClient;
    private final GeoFeignClient geoFeignClient;

    @Autowired
    public DepotController(@Qualifier("depotFeignClient") DepotFeignClient depotFeignClient,
                           @Qualifier("geoFeignClient") GeoFeignClient geoFeignClient) {
        this.depotFeignClient = depotFeignClient;
        this.geoFeignClient = geoFeignClient;
    }

    @PreAuthorize("hasAuthority('OPT_DEPOT_LIST')")
    @GetMapping({"", "list"})
    public ModelAndView list(@ModelAttribute("reqVo") DepotQueryReqVo reqVo) {
        List<AreaResponseVo> provinces = geoFeignClient.findAllProvince();
        List<AreaResponseVo> cities = geoFeignClient.findCitiesByProvinceId(reqVo.getProvinceId());
        List<AreaResponseVo> districts = geoFeignClient.findDistrictsByCityId(reqVo.getCityId());
        SpringPageVO<DepotListRespVo> pageResult = depotFeignClient.searchDepots(reqVo);
        return new ModelAndView("depot/list")
                .addObject("pageResult", pageResult)
                .addObject("provinces", provinces)
                .addObject("cities", cities)
                .addObject("districts", districts);
    }

    @GetMapping("detail/{depotId}")
    @PreAuthorize("hasAuthority('OPT_DEPOT_DETAIL')")
    public ModelAndView Detail(@PathVariable("depotId") Long depotId) {
        DepotDetailRespVo depot = depotFeignClient.findByIdFDb(depotId);
        return new ModelAndView("depot/detail")
                .addObject("depot", depot);
    }

    @PostMapping("updateStatus")
    @PreAuthorize("hasAuthority('OPT_DEPOT_UPDATE')")
    public ModelAndView updateDepotStatus(@RequestParam("depotId") Long depotId,
                                          @RequestParam(value = "businessStatus", defaultValue = "ON") BusinessStatus businessStatus) {
        depotFeignClient.updateBusinessStatus(depotId, businessStatus);
        return list(new DepotQueryReqVo());
    }


}
